package com.diagnosis.record;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Looper;
import android.util.Base64;
import android.util.Size;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.camera.core.CameraSelector;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.google.common.util.concurrent.ListenableFuture;

import org.json.JSONObject;


import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;


import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class RecordModule extends UniModule {

    //HX3.0.7版本云打包及相应版本SDK开始将Activity变更为FragmentActivity
    @UniJSMethod(uiThread = true)
    public String test() {
        if (mUniSDKInstance == null) {
            return "mUniSDKInstance =null";
        }
        if (!(mUniSDKInstance.getContext() instanceof Activity)) {
            return "mUniSDKInstance is not actvity ";
        }
        Toast.makeText(mUniSDKInstance.getContext(), "你好uniapp?", Toast.LENGTH_LONG).show();
        return "hello uniapp";
    }

    @UniJSMethod(uiThread = false)
    public String test2() {
        if (mUniSDKInstance == null) {
            return "mUniSDKInstance =null";
        }
        if (!(mUniSDKInstance.getContext() instanceof Activity)) {
            return "mUniSDKInstance is not actvity ";
        }
        Toast.makeText(mUniSDKInstance.getContext(), "你好uniapp?", Toast.LENGTH_LONG).show();
        return "hello uniapp";
    }

    private ProcessCameraProvider cameraProvider;
    private Preview previewUseCase;
    private PreviewView previewView;

    private CameraSelector cameraSelector;

    @UniJSMethod
    public void startCamera() {
        cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
        starrtCamear();
    }

    @UniJSMethod
    public void setlayout(int w, int h, int top, int left) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) previewView.getLayoutParams();
        layoutParams.width = w;
        layoutParams.height = h;
        layoutParams.topMargin = top;
        layoutParams.leftMargin = left;
        previewView.setLayoutParams(layoutParams);
    }


    private void starrtCamear() {
        Application application = (Application) mUniSDKInstance.getContext().getApplicationContext();
        ListenableFuture<ProcessCameraProvider> future = ProcessCameraProvider.getInstance(application);
        future.addListener(() -> {
            try {
                cameraProvider = future.get();
                if (cameraProvider != null) {
                    cameraProvider.unbindAll();
                    bindPreviewUseCase();
                }

            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, ContextCompat.getMainExecutor(application));
    }

    private void bindPreviewUseCase() {

        FragmentActivity context = (FragmentActivity) mUniSDKInstance.getContext();
        if (cameraProvider == null) {
            return;
        }
        if (previewUseCase != null) {
            cameraProvider.unbind(previewUseCase);
        }

        Preview.Builder builder = new Preview.Builder();
        builder.setTargetResolution(new Size(480, 640));

        previewUseCase = builder.build();

        previewView = new PreviewView(context);
        previewView.setImplementationMode(PreviewView.ImplementationMode.COMPATIBLE);

        previewUseCase.setSurfaceProvider(previewView.getSurfaceProvider());

        cameraProvider.bindToLifecycle(
                context,
                cameraSelector,
                previewUseCase
        );
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                300,
                300);
        layoutParams.leftMargin = 400;
        layoutParams.topMargin = 200;
        ViewGroup viewGroup = context.getWindow().getDecorView().findViewById(android.R.id.content);
        viewGroup.addView(previewView, layoutParams);
    }

    @UniJSMethod
    public String isFragmentActivity() {
        Context context = mUniSDKInstance.getContext();
        if (context instanceof FragmentActivity) {
            return "yes";
        } else if (context instanceof Activity) {
            return "is Activity";
        } else {
            return "no";
        }
    }

    @UniJSMethod
    public String isViewGroup() {
        FragmentActivity context = (FragmentActivity) mUniSDKInstance.getContext();

        ViewGroup viewGroup = context.getWindow().getDecorView().findViewById(android.R.id.content);
        if (viewGroup == null) {
            return "null";
        }

        return "not null";
    }

    @UniJSMethod
    public String isthreeData() {
        String data = "have";
        try {
            new PreviewView(mUniSDKInstance.getContext());
        } catch (Exception e) {
            data= "no "+e.getMessage();
        }
        return data;
    }


}
